package io.renren.modules.generator.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.generator.entity.BsnGoutongzhidaoEntity;
import io.renren.modules.generator.entity.BsnMentorEntity;
import io.renren.modules.generator.entity.ViewGoutongzhidao;
import io.renren.modules.generator.service.BsnGoutongzhidaoService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShortUuidUtils;



/**
 * 指导历史表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-07-03 19:08:41
 */
@RestController
@RequestMapping("generator/bsngoutongzhidao")
public class BsnGoutongzhidaoController {
	public static String ZT_ZHENGCHANG = "正常";
	public static String ZT_QUXIAO = "取消";
	
    @Autowired
    private BsnGoutongzhidaoService bsnGoutongzhidaoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
    	EntityWrapper<BsnGoutongzhidaoEntity> wrapper = new EntityWrapper<BsnGoutongzhidaoEntity>();
    	wrapper.gt("time2", new Date());
    	
    	params.put("sidx", "time1");
    	params.put("order", "desc");
        PageUtils page = bsnGoutongzhidaoService.queryPage(params, wrapper);
        this.fillStudentName(page);
        return R.ok().put("page", page);
    }

    /**
     * 已完成列表
     */
    @RequestMapping("/listFinished")
    public R listFinished(@RequestBody Map<String, Object> params){
    	EntityWrapper<BsnGoutongzhidaoEntity> wrapper = new EntityWrapper<BsnGoutongzhidaoEntity>();
    	wrapper.le("time2", new Date());
    	
    	params.put("sidx", "time1");
    	params.put("order", "desc");
        PageUtils page = bsnGoutongzhidaoService.queryPage(params, wrapper);
        this.fillStudentName(page);
        return R.ok().put("page", page);
    }    
    
    /**
     * 列表
     */
    @RequestMapping("/listByStudents")
    public R listByStudent(@RequestBody Map<String, Object> params){
    	Integer page = Integer.valueOf(params.get("page").toString());
    	Integer limit = Integer.valueOf(params.get("limit").toString());
    	ArrayList<String> studentIds = (ArrayList<String>)params.get("studentIds");

    	List<ViewGoutongzhidao> entityList = bsnGoutongzhidaoService.queryPageByStudentId(
    			studentIds, page, limit);
         
        return R.ok().put("list", entityList);
    }
    
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		BsnGoutongzhidaoEntity bsnGoutongzhidao = bsnGoutongzhidaoService.selectById(id);
		this.fillStudentName(bsnGoutongzhidao);
        return R.ok().put("bsnGoutongzhidao", bsnGoutongzhidao);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BsnGoutongzhidaoEntity bsnGoutongzhidao){
    	bsnGoutongzhidao.setId(ShortUuidUtils.generateShortUuid());
    	bsnGoutongzhidao.setZhuangtai(ZT_ZHENGCHANG);
		bsnGoutongzhidaoService.insert(bsnGoutongzhidao);
        return R.ok().put("bsnGoutongzhidao", bsnGoutongzhidao);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BsnGoutongzhidaoEntity bsnGoutongzhidao){
		bsnGoutongzhidaoService.updateById(bsnGoutongzhidao);
        return R.ok().put("bsnGoutongzhidao", bsnGoutongzhidao);
    }
    
    /**
     * 修改
     */
    @RequestMapping("/updateGoutong")
    public R updateGoutong(@RequestBody BsnGoutongzhidaoEntity bsnGoutongzhidao){
    	BsnGoutongzhidaoEntity one = new BsnGoutongzhidaoEntity();
    	one.setZhidaoneirong(bsnGoutongzhidao.getZhidaoneirong());
    	
		bsnGoutongzhidaoService.update(one, 
				new EntityWrapper<BsnGoutongzhidaoEntity>().eq("id", bsnGoutongzhidao.getId()));
        return R.ok();
    }
    
    /**
     * 取消预约
     */
    @RequestMapping("/cancel/{id}")
    public R cancel(@PathVariable("id") String id){
    	BsnGoutongzhidaoEntity bsnGoutongzhidao = new BsnGoutongzhidaoEntity();
    	bsnGoutongzhidao.setZhuangtai(ZT_QUXIAO);
		bsnGoutongzhidaoService.update(bsnGoutongzhidao, 
				new EntityWrapper<BsnGoutongzhidaoEntity>().eq("id", id));
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:bsngoutongzhidao:delete")
    public R delete(@RequestBody String[] ids){
			bsnGoutongzhidaoService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    @SuppressWarnings("unchecked")
	private void fillStudentName(PageUtils page) {
        List<BsnGoutongzhidaoEntity> zhidaoList = (List<BsnGoutongzhidaoEntity>) page.getList(); 
        for (BsnGoutongzhidaoEntity zhidao : zhidaoList) {
        	this.fillStudentName(zhidao);
		}
    }
    
    private void fillStudentName(BsnGoutongzhidaoEntity zhidao) {
    	zhidao.setStundentsid(
    			bsnGoutongzhidaoService.queryStudentNameByzhidaoId(zhidao.getId()));
    }
}
